﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataTransferObject.DiaryAndTrackingTask.SupportClassForDiary;

namespace JSKE.DiaryAndTracking
{
    public partial class FrmEditEvent : FormLang
    {
        public JskeEvent Evt;

        public FrmEditEvent()
        {
            //InitializeComponent();
            cbbPriority.DataSource = Enum.GetValues(typeof(EventPriority));
            cbbSatisfy.DataSource = Enum.GetValues(typeof(EventSatsified));
        }
        public FrmEditEvent(JskeEvent evt, object eventtypes)
        {
            //InitializeComponent();
            cbbPriority.DataSource = Enum.GetValues(typeof(EventPriority));
            cbbSatisfy.DataSource = Enum.GetValues(typeof(EventSatsified));
            cbbEventType.DataSource = eventtypes;

            this.Evt = evt;

            dtpkStart.Value = evt.EventStart;
            txtEventName.Text = evt.EventName;
            cbbEventType.Text = evt.EventType;
            txtDuration.Text = Convert.ToString(evt.EventDuration);
            cbbPriority.SelectedIndex = (int)evt.EventPriority;
            cbbSatisfy.SelectedIndex = (int)evt.EventSatisfy;
            txtMoney.Text = Convert.ToString(evt.Money);
            rtbContent.Rtf = evt.Notes;
        }
        private void btLeft_Click(object sender, EventArgs e)
        {
            btLeft.Checked = false;
            btCenter.Checked = false;
            btRight.Checked = false;
            ToolStripButton myButton = (ToolStripButton)sender;
            myButton.Checked = true;
            rtbContent.SelectionAlignment = (HorizontalAlignment)int.Parse(myButton.Tag.ToString());
        }

        private void btnFont_Click(object sender, EventArgs e)
        {
            FontDialog fDlg = new FontDialog();
            fDlg.Font = rtbContent.SelectionFont;
            fDlg.Color = rtbContent.SelectionColor;

            fDlg.ShowColor = true;
            if (fDlg.ShowDialog() == DialogResult.Cancel) return;
            rtbContent.SelectionFont = fDlg.Font;
            rtbContent.SelectionColor = fDlg.Color;
        }

        private void btnBullet_Click(object sender, EventArgs e)
        {
            rtbContent.SelectionBullet = btnBullet.Checked;
        }

        private void btnBIndent_DropDownClosed(object sender, EventArgs e)
        {
            rtbContent.BulletIndent = 5 * int.Parse(txtIndent.Text);
            rtbContent.SelectionIndent = 5 * int.Parse(txtAlignment.Text);
        }

        private void txtIndent_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) || e.KeyChar == 8) return;
            e.KeyChar = '\0';
        }

        private void richTextBox1_SelectionChanged(object sender, EventArgs e)
        {
            ToolStripButton myButton;
            switch (rtbContent.SelectionAlignment)
            {
                case HorizontalAlignment.Center:
                    myButton = btCenter;
                    break;
                case HorizontalAlignment.Left:
                    myButton = btLeft;
                    break;
                default:
                    myButton = btRight;
                    break;
            }
            if (!myButton.Checked) myButton.PerformClick();
            btnBullet.Checked = rtbContent.SelectionBullet;
            if (btnBullet.Checked) txtIndent.Text = "" + rtbContent.SelectionHangingIndent / 5;

            txtAlignment.Text = "" + rtbContent.SelectionIndent / 5;
            btnBold.Checked = (rtbContent.SelectionFont != null) && rtbContent.SelectionFont.Bold;
            btnItalic.Checked = (rtbContent.SelectionFont != null) && rtbContent.SelectionFont.Italic;
            btnUnderline.Checked = (rtbContent.SelectionFont != null) && rtbContent.SelectionFont.Underline;
        }

        private void btnWordWrap_Click(object sender, EventArgs e)
        {
            rtbContent.WordWrap = btnWordWrap.Checked;
        }

        private void btnBold_Click(object sender, EventArgs e)
        {
            FontStyle style = rtbContent.SelectionFont.Style;
            style = btnBold.Checked ? style | FontStyle.Bold : style & ~FontStyle.Bold;
            style = btnItalic.Checked ? style | FontStyle.Italic : style & ~FontStyle.Italic;
            style = btnUnderline.Checked ? style | FontStyle.Underline : style & ~FontStyle.Underline;
            Font f = new Font(rtbContent.SelectionFont, style);
            rtbContent.SelectionFont = f;
        }

        private void mnRed_Click(object sender, EventArgs e)
        {
            rtbContent.SelectionColor = ((ToolStripMenuItem)sender).ForeColor;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void BtnOK_Click(object sender, EventArgs e)
        {
            if (cbbEventType.Items.Count < 10)
            {
                MessageBox.Show("Unimplemented Feature for parse Appointment NLP");
            }
            try
            {
                Evt.Money = Double.Parse(txtMoney.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Money must be a number: "+ex.Message, "Notice", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            Evt.EventDuration = int.Parse(txtDuration.Text);
            txtDuration.Text = "";
            Evt.EventName = txtEventName.Text;
            txtEventName.Text = "";
            Evt.EventPriority = (EventPriority)cbbPriority.SelectedIndex;
            Evt.EventSatisfy = (EventSatsified)cbbSatisfy.SelectedItem;
            Evt.EventStart = dtpkStart.Value;
            Evt.EventType = cbbEventType.Text;
            Evt.Notes = rtbContent.Rtf;

            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        public void setRTF(String rtf)
        {
            Evt.Notes = rtf;
        }
    }
}
